package com.luntan.service.impl;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.luntan.domain.TUser;
import com.luntan.service.TUserService;
import com.luntan.util.web.Page;

/**
 * 用户的业务逻辑层（接口）
 *
 */
public class TUserServiceImpl extends BaseServiceImpl<TUser, Integer> implements TUserService {

	@Override
	public boolean addSave(TUser tUser) {
		boolean flag = false;
		try {
			this.baseDAO.saveEntity(tUser);
			flag = true;
		} catch (Exception ex) {
			Logger.getLogger(TUser.class.getName()).log(Level.SEVERE, null, ex);
		}
		return flag;
	}

	@Override
	public boolean updateSave(TUser tUser) {
		boolean flag = false;
		try {
			this.baseDAO.updateEntity(tUser);
			flag = true;
		} catch (Exception ex) {
			Logger.getLogger(TUser.class.getName()).log(Level.SEVERE, null, ex);
		}
		return flag;
	}

	@Override
	public TUser update(Integer id) {
		try {
			TUser tUser = this.baseDAO.getEntity(TUser.class, id);
			return tUser;
		} catch (Exception ex) {
			Logger.getLogger(TUser.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}

	@Override
	public boolean delete(Integer id) {
		boolean flag = false;
		try {
			TUser tUser = this.baseDAO.getEntity(TUser.class, id);
			this.baseDAO.removeEntity(tUser);
			flag = true;
		} catch (Exception ex) {
			Logger.getLogger(TUser.class.getName()).log(Level.SEVERE, null, ex);
		}
		return flag;
	}

	@Override
	public Page<TUser> getList(int page, int limit, TUser tUser) {
		StringBuilder hsql = new StringBuilder();
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		hsql.append("FROM TUser AS m ");

		try {
			Page<TUser> list = this.baseDAO.search(hsql.append(where).toString(), page, limit);
			return list;
		} catch (Exception ex) {
			Logger.getLogger(TUser.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}

	@Override
	public List<TUser> getList() {
		StringBuilder hsql = new StringBuilder();
		hsql.append("FROM TUser AS m ");
		try {
			List<TUser> list = this.baseDAO.findEntity(hsql.toString());
			return list;
		} catch (Exception ex) {
			ex.printStackTrace();
			Logger.getLogger(TUser.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}

	@Override
	public TUser get(TUser tUser) {
		if(tUser==null){
			return null;
		}
		
		StringBuilder hsql = new StringBuilder();
		hsql.append("FROM TUser AS m ").append(" where 1=1 ");
		
		if(tUser.getUserName()!=null&&!"".equals(tUser.getUserName())){
			hsql.append(" AND m.userName='").append(tUser.getUserName()).append("' ");
		}
		
		if(tUser.getPassword()!=null&&!"".equals(tUser.getPassword())){
			hsql.append(" AND m.password='").append(tUser.getPassword()).append("' ");
		}
		
		try {
			List<TUser> list = this.baseDAO.findEntity(hsql.toString());
			if(list.size()>0){
				return list.get(0);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			Logger.getLogger(TUser.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}

}
